﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using CADTool.DotNetARX;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;

namespace CADTool.Jig
{

    public class CirclJig
    {
        [CommandMethod("JigCircle")]
        public void JigCircleText()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed=Application.DocumentManager.MdiActiveDocument.Editor;
            Matrix3d mt = ed.CurrentUserCoordinateSystem;
            Vector3d noraml = mt.CoordinateSystem3d.Zaxis;
            //初始化圆
            CircleJig circleJig = new CircleJig(noraml);
            for (; ; )
            {
                //拖拽
                PromptResult resJig = ed.Drag(circleJig);
                if (resJig.Status==PromptStatus.Cancel)
                {
                    return;
                }
                if (resJig.Status==PromptStatus.OK)
                {
                    Tools.AddToModelSpace(db,circleJig.GetEntity());
                    break;
                }
            }
        }
    }
  

    public class CircleJig : EntityJig
    {
        private Point3d m_CenterPt; //圆的中心点
        private double m_Radius = 100.0;//圆的默认半径
        //派生类的构造函数
        public CircleJig(Vector3d normal) : base(new Circle())
        {
            ((Circle)Entity).Center = m_CenterPt;
            ((Circle)Entity).Normal = normal;
            ((Circle)Entity).Radius = m_Radius;
        }
        public Entity GetEntity()
        {
            return Entity;
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            //定义一个点拖拽交互类
            JigPromptPointOptions optJig=new JigPromptPointOptions("\n请指定圆心或者右键修改半径");
            //添加关键字
            optJig.Keywords.Add("100");
            optJig.Keywords.Add("200");
            optJig.Keywords.Add("300");
            optJig.UserInputControls = UserInputControls.Accept3dCoordinates;
            //用AcquirePoint函数得到用户输入的点
            PromptPointResult resJig=prompts.AcquirePoint(optJig);
            Point3d curPt = resJig.Value;
            //取消拖拽
            if (resJig.Status==PromptStatus.Cancel)
            {
                return SamplerStatus.Cancel;
            }
            //拖拽时输入关键字
            if (resJig.Status == PromptStatus.Keyword)
            { 
                switch(resJig.StringResult)
                {
                    case "100":
                        m_Radius = 100;
                        return SamplerStatus.NoChange;
                    case "200":
                        m_Radius = 200;
                        return SamplerStatus.NoChange;
                    case "300":
                        m_Radius = 300;
                        return SamplerStatus.NoChange;
                }
            }
            //拖拽
            if (m_CenterPt!=curPt)
            {
                //保存当前点
                m_CenterPt= curPt;
                return SamplerStatus.OK;
            }
            else
            {
                //拖拽停止
                return SamplerStatus.NoChange;
            }
        }

        protected override bool Update()
        {
            ((Circle)Entity).Center = m_CenterPt;
            ((Circle)Entity).Radius = m_Radius;
            return true;
        }
    }
}